<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>首页</title>
  <link rel="stylesheet" href="../plugins/elementui/index.css">
  <link rel="stylesheet" href="css/webbase.css">
  <style>
    body {
      background-color: #f5f5f5;
      color: #333;
    }

    .container {
      text-align: center;

      display: flex;
      justify-content: center;
      /* 整体居中 */
    }

    .product-display {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      /* 每行卡片居中 */
    }

    .product-card {
      background-color: white;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      margin: 10px;
      box-sizing: border-box;
      width: 460px;
      height: 295px;
    }

    .product-card h3 {
      margin: 10px 0;
      font-size: 14px;
      line-height: 1.2;
      height: 34px;
      /* 限制高度为两行 */
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      /* 限制在2行 */
      -webkit-box-orient: vertical;
    }

    @media (max-width: 1200px) {
      .container {
        width: 100%;
      }
    }

    .floating-announcement {
      position: fixed;
      bottom: 20px;
      right: 20px;
      background-color: rgba(255, 255, 255, 0.9);
      border: 0px solid #ddd;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      padding: 10px;
      max-width: 300px;
      z-index: 1000;
    }

    [v-cloak] {
      display: none;
    }
  </style>
</head>

<body>
  <div id="app">
    <el-container>
      <app-header @search="handleSearch"></app-header>
      <el-main>
        <div class="container">
          <!-- 添加一个局部loading遮罩层，仅应用于product-display -->
          <div class="product-display" v-loading="loading" v-cloak>
            <div class="product-card" v-for="product in products" :key="product.id" @click="toDetail(product.id)">
              <img :src="product.imageUrl" alt="Product Image" style="width: 100%;height: 216px;object-fit: cover ">
              <h3>{{ product.name }}</h3>
            </div>
            <!-- 当没有数据时显示提示 -->
            <div v-if="!loading && products.length === 0" style="text-align:center;">
              暂无商品信息
            </div>
          </div>
        </div>
        <div style="width: 100%;text-align: center;position: fixed;bottom: 0px">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page.sync="params.pageNo" :page-size="params.pageSize" layout="total, prev, pager, next"
            :total="total">
          </el-pagination>
        </div>
      </el-main>
    </el-container>
    <div class="floating-announcement" v-if="announcement && showAnnouncement" v-cloak>
      <div style="width: 100%;text-align: right;cursor: pointer" @click="showAnnouncement = false">x</div>
      <pre>{{ announcement }}</pre>
    </div>
  </div>
  <script src="../js/vue@2.js"></script>
  <script src="../js/axios-0.18.0.js"></script>
  <script src="../plugins/elementui/index.js"></script>
  <script src="../js/top.js"></script>
  <script>
    new Vue({
      el: '#app',
      data() {
        return {
          loading: true,
          announcement: null,
          showAnnouncement: true,
          products: [],
          params: {
            pageNo: 1,
            pageSize: 50,
            name: ''
          },
          total: 0
        }
      },
      mounted() {
        // 检查URL中是否有搜索参数
        const urlParams = new URLSearchParams(window.location.search);
        const searchQuery = urlParams.get('search');
        if (searchQuery) {
          this.params.name = searchQuery;
          this.init();
        }
      },
      methods: {
        handleSearch(query) {
          this.params.name = query;
          this.params.pageNo = 1;
          this.loading = true;
          this.init();
        },
        toDetail(id) {
          window.location.href = 'gameDetail.html?id=' + id
        },
        handleSizeChange() {
          this.loading = true;
          this.init()
        },
        handleCurrentChange() {
          this.loading = true;
          this.init()
        },

        buyNow(productId) {
          window.location.href = `order-confirm.html?productId=${productId}`;
        },
        async init() {
          try {
            // 构建查询参数
            const queryParams = {
              pageNo: this.params.pageNo,
              pageSize: this.params.pageSize
            };

            // 只有当有搜索内容时才添加name参数
            if (this.params.name) {
              queryParams.name = this.params.name;
            }

            // 发送请求
            const res = await axios.get("com/game/page", {
              params: queryParams
            });
            this.products = res.data.records;
            this.total = res.data.total;

            // 获取公告内容
            const announcementRes = await axios.get("/announcement/page", {
              params: {
                pageNo: 1,
                pageSize: 1
              }
            });
            this.announcement = announcementRes.data.records[0].content;
          } catch (error) {
            console.error('获取数据失败:', error);
            this.$message.error('获取数据失败');
          } finally {
            this.loading = false;
          }
        }
      },
      created() {
        this.init();
      }
    });
  </script>
</body>

</html>